<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 国外cdn -->
  <!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> -->


  <!-- 国内cdn -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
</head>
<style>
  * {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }

  body {
    height: 100vh;
    background-color: #dadada;
    display: flex;
  }

  .wrapper {
    width: 1200px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    margin: auto;
  }

  .image {
    width: 22%;
    height: 200px;
    overflow: hidden;
    margin: 10px;
    cursor: pointer;
    border-radius: 4px;
  }

  .image:hover img {
    transform: scale(1.2);
  }

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
    transition: 0.25s;
  }

  .gallery {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    z-index: 1;
    display: flex;
    opacity: 0;
    pointer-events: none;
    transform: scale(0.8);
    transition: 0.25s;
  }

  .gallery.show {
    opacity: 1;
    pointer-events: auto;
    transform: scale(1);
  }

  .gallery-inner {
    width: 70%;
    height: 70%;
    margin: auto;
  }

  .control {
    position: absolute;
    color: rgba(255, 255, 255, 0.8);
    font-size: 50px;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
  }

  .prev {
    left: 15px;
  }

  .next {
    right: 15px;
  }

  .close {
    color: white;
    font-size: 35px;
    position: absolute;
    top: 15px;
    right: 25px;
    cursor: pointer;
  }

  .hide {
    display: none;
  }
</style>

<body>
  <div class="wrapper">
    <div class="image">
      <img
        src="https://images.unsplash.com/photo-1554995207-c18c203602cb?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80"
        alt="" />
    </div>
    <div class="image">
      <img
        src="https://images.unsplash.com/photo-1600596542815-ffad4c1539a9?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1475&q=80"
        alt="" />
    </div>
    <div class="image">
      <img
        src="https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80"
        alt="" />
    </div>
    <div class="image">
      <img
        src="https://images.unsplash.com/photo-1564013799919-ab600027ffc6?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80"
        alt="" />
    </div>
    <div class="image">
      <img
        src="https://images.unsplash.com/photo-1512917774080-9991f1c4c750?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80"
        alt="" />
    </div>
    <div class="image">
      <img
        src="https://plus.unsplash.com/premium_photo-1678297269980-16f4be3a15a6?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80"
        alt="" />
    </div>
    <div class="image">
      <img
        src="https://images.unsplash.com/photo-1576941089067-2de3c901e126?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1578&q=80"
        alt="" />
    </div>
    <div class="image">
      <img
        src="https://images.unsplash.com/photo-1513584684374-8bab748fbf90?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1465&q=80"
        alt="" />
    </div>
  </div>

  <div class="gallery">
    <span class="control prev">
      <i class="fa fa-angle-double-left"></i>
    </span>
    <span class="control next">
      <i class="fa fa-angle-double-right"></i>
    </span>
    <div class="gallery-inner">
      <img src="" alt="" />
    </div>
    <i class="fa fa-close close"></i>
  </div>
</body>
<script>
  const images = document.querySelectorAll(".wrapper .image img");
  const gallery = document.querySelector(".gallery");
  const galleryImg = document.querySelector(".gallery-inner img");
  const close = document.querySelector(".gallery .close");

  const next = document.querySelector(".control.next");
  const prev = document.querySelector(".control.prev");

  let currentIndex = 0;

  images.forEach((img, index) => {
    img.addEventListener("click", () => {
      currentIndex = index;
      showGallery();
    });
  });

  function showGallery() {
    currentIndex == images.length - 1
      ? next.classList.add("hide")
      : next.classList.remove("hide");

    currentIndex == 0
      ? prev.classList.add("hide")
      : prev.classList.remove("hide");

    gallery.classList.add("show");
    galleryImg.src = images[currentIndex].src;
  }

  close.addEventListener("click", () => {
    gallery.classList.remove("show");
  });

  next.addEventListener("click", () => {
    currentIndex != images.length - 1 ? currentIndex++ : undefined;
    showGallery();
  });
  prev.addEventListener("click", () => {
    currentIndex != 0 ? currentIndex-- : undefined;
    showGallery();
  });

  // esc click
  document.addEventListener("keydown", (e) => {
    if (e.keyCode == 27) gallery.classList.remove("show");
  });
</script>

</html>